﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using RoboRace_Shared;
using NetSockets;

namespace RoboRace_Client
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    /// 
    
    public partial class MainWindow : Window
    {
        NetObjectClient netClient;
        Board gameboard;
        Player player;
        List<Robot> robots;

        public MainWindow()
        {
            InitializeComponent();
            netClient = new NetObjectClient();
            
            netClient.OnReceived += new NetReceivedEventHandler<NetObject>(netClient_OnReceived);
        }

        void netClient_OnReceived(object sender, NetReceivedEventArgs<NetObject> e)
        {
            switch (e.Data.Name)
            {
                case "Guid":
                    player.Guid = (Guid)e.Data.Object;
                    break;
                case "Board":
                    gameboard = (Board)e.Data.Object;
                    break;
                case "Robots":
                    robots = (List<Robot>)e.Data.Object;
                    break;
                case "Notification":
                    MessageBox.Show(e.Data.Object.ToString());
                    break;                    
            }

            UpdateGUI();
        }

        private void UpdateGUI()
        {
            
        }

        private void win_Loaded(object sender, RoutedEventArgs e)
        {   

        }

        private void StartButton_Click(object sender, RoutedEventArgs e)
        {
            player = new Player(PlayerName.Text.ToString());
            netClient.TryConnect("127.0.0.1", 3333);
            btnStart.IsEnabled = false;
            btnJoin.IsEnabled = true;
        }

        private void btnJoin_Click(object sender, RoutedEventArgs e)
        {
            NetObject obj = new NetObject("Join", player);
            netClient.Send(obj);
            btnJoin.IsEnabled = false;
            btnDisconnect.IsEnabled = true;
        }

        private void btnDisconnect_Click(object sender, RoutedEventArgs e)
        {
            btnDisconnect.IsEnabled = false;
            btnStart.IsEnabled = true;
            netClient.Disconnect();
        }
    }
}
